package com.sw.haruka.helper.utils

import android.content.Context
import android.content.SharedPreferences

object Configuration {

    enum class Keys {
        ExplorerAnimationSwitch, FavoriteSet, ShowHiddenFile
    }

    fun initialize(context: Context) {
        preferences = context.getSharedPreferences("app_config", Context.MODE_PRIVATE)
    }

    private var preferences: SharedPreferences? = null

    fun getIntDefaultZero(key: String) = getPreferences().getInt(key, 0)

    fun saveInt(key: String, value: Int) = getEditor().putInt(key, value).apply()

    fun saveString(key: String, string: String?) = getEditor().putString(key, string).apply()

    fun getBoolean(key: String) = getPreferences().getBoolean(key, false)

    fun getBoolean(key: Enum<*>) = getBoolean(key.name)

    fun saveBoolean(key: String, value: Boolean) = getEditor().putBoolean(key, value).apply()

    fun saveBoolean(key: Enum<*>, value: Boolean) = getEditor().putBoolean(key.name, value).apply()

    fun getStringDefaultNull(key: String) = getPreferences().getString(key, null)

    fun getStringDefaultNull(key: Enum<*>) = getPreferences().getString(key.name, null)

    fun getStringSetDefaultEmpty(key: String): Set<String> {
        return HashSet(getPreferences().getStringSet(key, HashSet()))
    }

    fun getStringSetDefaultEmpty(key: Enum<*>) = getStringSetDefaultEmpty(key.name)

    fun saveSet(key: String, value: Set<String>) = getEditor().putStringSet(key, HashSet(value)).apply()

    fun saveSet(key: Enum<*>, value: Set<String>) = saveSet(key.name, value)

    private fun getEditor(): SharedPreferences.Editor {
        return getPreferences().edit()
    }

    private fun getPreferences(): SharedPreferences = preferences!!

}